package cn.keniykey.mc.landterritory.landview;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Nullable;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration.ViewConfig;
import cn.keniykey.mc.landterritory.event.LandFlagRestEvent;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.api.ELandType;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandRange;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.land.api.LandUtil.ELandPermissionLevel;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;
import cn.keniykey.mc.viewlibrary.view.api.template.AbChooseView;
import cn.keniykey.mc.viewlibrary.view.api.template.ChooseViewIntentBuilder;

public final class LandFlagRestView extends AbLandFrameView{

	protected LandFlagRestView(Player player1) {
		super(player1);
	}
	
	private static final String VIEW_NAME = "flagrest";
	
	private static final int CODE_CHOOSE_REST = 10300;
	private static final int CODE_CHOOSE_COVER = 10301;
	private static final int CODE_CHOOSE_SYNCH = 10302;
	
	private Land land,land_community;
	
	@Override
	protected void onCreate() {
		super.onCreate();
		this.land = this.getIntent().getExtras().getValue("land", null);
		this.land_community = this.getCommunityWithGround();
		
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		this.setViewType(EContainerType.matchContainer(config_view.getViewSize()));
		this.setTitle(Util.conver(config_view.getViewTitle()));
		
		this.setIconClick(config_view.getViewItemSlot("back"), config_view.getViewItem("back"), AbLandFrameView.CLICK_BACK);
		this.setIconClick(config_view.getViewItemSlot("rest"), config_view.getViewItem("rest"), new IViewClickHandler() {	
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandFlagRestView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1103101, LanguageManager.getLang(1103100)));
				builder.setCustomPromptLore(Util.listFromStr(LanguageManager.getLang(1103102,
						LandViewUtil.getLandLabelStr(LandFlagRestView.this.land))));
				LandFlagRestView.this.showViewForResult(builder.build(), CODE_CHOOSE_REST);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("cover"), config_view.getViewItem("cover"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandFlagRestView.this.land_community == null) return;
				if(!LandFlagRestView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1103101, LanguageManager.getLang(1103150)));
				builder.setCustomPromptLore(Util.listFromStr(LanguageManager.getLang(1103151,
						LandViewUtil.getLandLabelStr(LandFlagRestView.this.land),
						LandViewUtil.getLandLabelStr(LandFlagRestView.this.land_community))));
				LandFlagRestView.this.showViewForResult(builder.build(), CODE_CHOOSE_COVER);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("synch"), config_view.getViewItem("synch"), new IViewClickHandler() {	
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandFlagRestView.this.land_community == null) return;
				if(!LandFlagRestView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1103101, LanguageManager.getLang(1103200)));
				builder.setCustomPromptLore(Util.listFromStr(LanguageManager.getLang(1103151,
						LandViewUtil.getLandLabelStr(LandFlagRestView.this.land),
						LandViewUtil.getLandLabelStr(LandFlagRestView.this.land_community))));
				LandFlagRestView.this.showViewForResult(builder.build(), CODE_CHOOSE_SYNCH);
			}
		});
		
		final ItemStack item_about = config_view.getViewItem("about");
		final ItemMeta meta = item_about.getItemMeta();
		meta.setLore(Util.listFromStr(LanguageManager.getLang(1103000,LandViewUtil.getLandLabelStr(this.land),
				(this.land_community == null ? LanguageManager.getLang(1103001) : LandViewUtil.getLandLabelStr(this.land_community)))));
		item_about.setItemMeta(meta);
		this.setIcon(config_view.getViewItemSlot("about"), item_about);
	}
	
	@Nullable
	private Land getCommunityWithGround() {
		final LandRange range = this.land.getRange();
		final LinkedList<Land> lands_collision = LandManager.getLandManager().getCollisionLands(range);
		Land land_community = null;
		for(final Land land : lands_collision) {
			if(!isCommunityAccordWithGround(this.land, land)) continue;
			land_community = land;
			break;
		}
		return land_community;
	}
	
	@Override
	protected void onViewResult(int requestcode, int resultcode, @Nullable FrameViewBundle bundle) {
		super.onViewResult(requestcode, resultcode, bundle);
		switch(AbChooseView.EChooseResult.muchResult(resultcode)) {
		case CONFIRM:
			if(CODE_CHOOSE_REST == requestcode) {
				LandFlagListView.restLandFlag(this.getPlayer(),this.land);
				break;
			}
			if(CODE_CHOOSE_COVER == requestcode) {
				coverLandFlag(this.getPlayer(), this.land, this.land_community);
				break;
			}
			if(CODE_CHOOSE_REST == requestcode) {
				synchLandFlag(this.getPlayer(), this.land, this.land_community);
				break;
			}
			this.getPlayer().sendMessage(LanguageManager.getChatLang(1001001) + requestcode);
			break;
		case VETO: return;
		default:
		}
		this.finishAll();
	}
	
	private static boolean isCommunityAccordWithGround(final Land land_ground,final Land land) {
		if(!ELandType.COMMUNITY.equals(land.getType())) return false;
		if(!land_ground.getRange().isInRange(land.getRange())) return false;
		if(land_ground.isSystem() != land.isSystem()) return false;
		if(!land.isSystem() && !land_ground.getOwner().equals(land.getOwner())) return false;
		return true;
	}
	
	static void coverLandFlag(final Player player,final Land land_ground,final Land land_community) {
		if(!LandUtil.hasLandPermission(player, land_ground, ELandPermissionLevel.OWNER)) {
			player.sendMessage(LanguageManager.getChatLang(1005000, LandViewUtil.getLandLabelStr(land_ground), 
					LanguageManager.getLang(1103150)));
			return;
		}
		if(!isCommunityAccordWithGround(land_ground, land_community)) {
			player.sendMessage(LanguageManager.getChatLang(1103152, LandViewUtil.getLandLabelStr(land_community)));
			return;
		}
		
		for(Set<String> pars : land_ground.getFlags().values()) {
			if(!pars.isEmpty()) pars.clear();
		}
		land_ground.getFlags().clear();
		for(final String key : land_community.getFlags().keySet()) {
			land_ground.getFlags().put(key, Collections.emptySet());
		}
		//cover覆盖不包括领地标记参数
		
		land_ground.updateFlags();
		Bukkit.getPluginManager().callEvent(new LandFlagRestEvent(player, land_ground));
		player.sendMessage(LanguageManager.getChatLang(1103153, LandViewUtil.getLandLabelStr(land_community),
				LandViewUtil.getLandLabelStr(land_ground)));
	}
	static void synchLandFlag(final Player player,final Land land_ground,final Land land_community) {
		if(!LandUtil.hasLandPermission(player, land_ground, ELandPermissionLevel.OWNER)) {
			player.sendMessage(LanguageManager.getChatLang(1005000, LandViewUtil.getLandLabelStr(land_ground), 
					LanguageManager.getLang(1103200)));
			return;
		}
		if(!isCommunityAccordWithGround(land_ground, land_community)) {
			player.sendMessage(LanguageManager.getChatLang(1103152, LandViewUtil.getLandLabelStr(land_community)));
			return;
		}
		
		for(Set<String> pars : land_ground.getFlags().values()) {
			if(!pars.isEmpty()) pars.clear();
		}
		land_ground.getFlags().clear();
		for(final Entry<String, Set<String>> entry : land_community.getFlags().entrySet()) {
			final Set<String> pars = entry.getValue();
			land_ground.getFlags().put(entry.getKey(), (pars.isEmpty() ? Collections.emptySet() : new HashSet<>(pars)));
		}
		//synch同步包括领地标记的参数
		
		land_ground.updateFlags();
		Bukkit.getPluginManager().callEvent(new LandFlagRestEvent(player, land_ground));
		player.sendMessage(LanguageManager.getChatLang(1103201, LandViewUtil.getLandLabelStr(land_community),
				LandViewUtil.getLandLabelStr(land_ground)));
	}
}